{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "e35e2b69",
   "metadata": {},
   "source": [
    "-------\n",
    "* date:2022-11-14/week13  \n",
    "* 记录人：许丹"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d463121a",
   "metadata": {},
   "source": [
    "# Head first python练习"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7d604d0b",
   "metadata": {},
   "source": [
    "# 1.list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "9a84d505",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "i\n",
      "i\n",
      "a\n"
     ]
    }
   ],
   "source": [
    "vowels = ['a','e','i','o','u']\n",
    "word = \"miliways\"\n",
    "for letter in word:\n",
    "            if letter in vowels:\n",
    "                  print(letter)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "b2b85632",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "i\n",
      "i\n",
      "a\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "['i', 'i', 'a']"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# A-2\n",
    "vowels = ['a','e','i','o','u']\n",
    "found = []\n",
    "word = \"miliways\"\n",
    "for letter in word:\n",
    "            if letter in vowels:\n",
    "                print(letter)\n",
    "                found.append(letter)\n",
    "found"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "6a85d9c4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "don't panis!\n",
      "['d', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 's', '!']\n"
     ]
    }
   ],
   "source": [
    "# A-3\n",
    "phrase = \"don't panis!\"\n",
    "plist = list(phrase)\n",
    "print(phrase)\n",
    "print(plist)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "0d0afc30",
   "metadata": {},
   "outputs": [],
   "source": [
    "plist.remove('d')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "4edf9412",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 's', '!']"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "plist"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "2bc153b1",
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in range(3):\n",
    "    plist.pop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "ab89f3ae",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['o', 'n', \"'\", 't', ' ', 'p', 'a', 'n']"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "plist"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "1ecd0f0b",
   "metadata": {},
   "outputs": [],
   "source": [
    "plist.remove(\"'\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "915b4b47",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['o', 'n', 't', ' ', 'p', 'a', 'n']"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "plist"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "f7c9f14a",
   "metadata": {},
   "outputs": [],
   "source": [
    "plist.insert(3,plist[-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "b002b370",
   "metadata": {},
   "outputs": [],
   "source": [
    "plist.insert(2,plist[-3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "17978b9c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['o', 'n', 'p', 't', 'n', ' ', 'p', 'a', 'n']"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "plist"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "a6fc6b66",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'p'"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "plist.pop(-2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "e07f26b4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['o', 'n', 'p', 't', 'n', ' ', 'a']"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "plist"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "40615353",
   "metadata": {},
   "outputs": [],
   "source": [
    "new_phrase = ''.join(plist)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "30d6cd96",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'onptn a'"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "new_phrase"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "567fbf70",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "don't panic!\n",
      "['d', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n",
      "['o', 'n', 't', ' ', 'p', 'a']\n",
      "['o', 'n', 't', ' ', 'a', 'p']\n"
     ]
    },
    {
     "ename": "NameError",
     "evalue": "name 'pop' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Input \u001b[1;32mIn [29]\u001b[0m, in \u001b[0;36m<cell line: 13>\u001b[1;34m()\u001b[0m\n\u001b[0;32m     11\u001b[0m plist\u001b[38;5;241m.\u001b[39mextend([plist\u001b[38;5;241m.\u001b[39mpop(),plist\u001b[38;5;241m.\u001b[39mpop()])\n\u001b[0;32m     12\u001b[0m \u001b[38;5;28mprint\u001b[39m(plist)  \n\u001b[1;32m---> 13\u001b[0m plist\u001b[38;5;241m.\u001b[39minsert(\u001b[38;5;241m2\u001b[39m,plist,\u001b[43mpop\u001b[49m(\u001b[38;5;241m3\u001b[39m))\n\u001b[0;32m     14\u001b[0m \u001b[38;5;28mprint\u001b[39m(plist)\n\u001b[0;32m     15\u001b[0m \u001b[38;5;66;03m# 列表元素的合并：''.join()\u001b[39;00m\n",
      "\u001b[1;31mNameError\u001b[0m: name 'pop' is not defined"
     ]
    }
   ],
   "source": [
    "#A-3 整理(列表的方法)\n",
    "phrase = \"don't panic!\"\n",
    "plist = list(phrase)\n",
    "print(phrase)\n",
    "print(plist)\n",
    "for i in range(4):\n",
    "    plist.pop()\n",
    "plist.pop(0)\n",
    "plist.remove(\"'\")\n",
    "print(plist)\n",
    "plist.extend([plist.pop(),plist.pop()])\n",
    "print(plist)  \n",
    "plist.insert(2,plist,pop(3))\n",
    "print(plist)\n",
    "# 列表元素的合并：''.join()\n",
    "''.join(plist)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "fe8bfec3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "don't panic!\n",
      "['d', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n"
     ]
    }
   ],
   "source": [
    "#A-4 列表的切片和取值 on tap\n",
    "phrase = \"don't panic!\"\n",
    "plist = list(phrase)\n",
    "print(phrase)\n",
    "print(plist)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "16876dc9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'on tap'"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "''.join([plist[1],plist[2],plist[5],plist[4],plist[-5],plist[-6]])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4d03f87a",
   "metadata": {},
   "source": [
    "# 课后作业\n",
    "## 第三章：Structured Data练习"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "223d3507",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
